package com.asm;

public class DirectMemoryReference extends MemoryReference {
    protected Literal value;
    protected IntegerLiteral offset;
    boolean isGlobal;

    public DirectMemoryReference(Literal val) {
        this(val, 0, false);
    }

    public DirectMemoryReference(Literal val, long offset, boolean isGlobal) {
        IntegerLiteral integer = new IntegerLiteral(offset);
        this.value = val;
        this.offset = integer;
        this.isGlobal = isGlobal;
    }

    public DirectMemoryReference(Literal val, IntegerLiteral offset, boolean isGlobal) {
        this.value = val;
        this.offset = offset;
        this.isGlobal = isGlobal;
    }

    public Literal value() {
        return this.value;
    }

    public void collectStatistics(Statistics stats) {
        value.collectStatistics(stats);
    }

    public void fixOffset(long diff) {
        throw new Error("DirectMemoryReference#fixOffset");
    }

    public String toString() {
        return toSource(SymbolTable.dummy());
    }

    public String toSource(SymbolTable table) {
        String prefix = "";
        // if (isGlobal) {
        //     prefix = "=";
        // }
        if (offset.value() == 0) {
            return prefix + this.value.toSource(table);
        } else if (offset.value() > 0) {
            return prefix + this.value.toSource(table) + "+" + offset.toSource();

        } else {
            return prefix + this.value.toSource(table) + offset.toSource();
        }

    }

    public int compareTo(MemoryReference mem) {
        return -(mem.cmp(this));
    }

    protected int cmp(IndirectMemoryReference mem) {
        return 1;
    }

    protected int cmp(DirectMemoryReference mem) {
        return value.compareTo(mem.value);
    }

    public String dump() {
        return "(DirectMemoryReference " + value.dump() + ")";
    }

    @Override
    public int getOffset() {
        // TODO Auto-generated method stub
        throw new UnsupportedOperationException("Unimplemented method 'getOffset'");
    }
}
